package com.cdh.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdh.edu.entity.Subject;
import com.cdh.edu.entity.excel.SubjectData;
import com.cdh.edu.entity.subject.OneSubject;
import com.cdh.edu.entity.subject.TwoSubject;
import com.cdh.edu.entity.vo.SubjectVo;
import com.cdh.edu.mapper.SubjectMapper;
import com.cdh.edu.service.SubjectService;
import com.cdh.edu.service.listener.SubjectExcelListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cdh
 * @since 2020-10-19
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    //添加技术分类
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            // 调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OneSubject> getAllOneTwoSubject() {
        // 1.获取所有的一级分类
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        List<Subject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // 2.获取所有的二级分类
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        List<Subject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // 创建list集合，用于存储最终封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        // 3.封装一级分类
        // 查询出所有的一级分类list集合，遍历得到每个一级分类对象，获取每个一级分类对象的值
        // 封装到要求的list集合里面 finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {
            Subject subject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();

            // oneSubject.setId(subject.getId());
            // oneSubject.setTitle(subject.getTitle());
            // 把subject的值复制到oneSubject中
            BeanUtils.copyProperties(subject,oneSubject);

            finalSubjectList.add(oneSubject);

            // 在一级分类循环遍历查询所有二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                // 获取每个二级分类
                Subject tSubject = twoSubjectList.get(m);
                // 判断二级分类parent_id和一级分类的parent_id是否相同
                if (tSubject.getParentId().equals(oneSubject.getId())){
                    // 把tSubject放到twoSubject里，再添加到twoFinalSubjectList
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);

                    twoFinalSubjectList.add(twoSubject);
                }
            }
            // 把一级下面所有的二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }

    @Override
    public List<SubjectVo> nestedList() {
        return baseMapper.selectNestedListByParentId("0");
    }
}
